package com.twu.biblioteca;

import java.io.IOException;

public class BibliotecaApp {

    private MenuList[] menuList = new MenuList[6];
    private InputHandler inputHandler;

    public BibliotecaApp(){
        menuList[0] = new Gateway();
        menuList[1] = new BookList();
        menuList[2] = new MovieInfo();
        menuList[3] = new ReserveBook();
        menuList[4] = new UserInfo();
        menuList[5] = new AppSafeExit();

        inputHandler = new InputHandler();
    }

    public void printWelcomeMessage() {
        System.out.println("\nWelcome to Biblioteca!\n");
    }

    public void run() throws IOException {
        int input = 0;

        printWelcomeMessage();

        do{
            System.out.println("\nMain Menu List\n");

            for(int i=0; i<menuList.length; i++)
                System.out.println((i + 1) + " ) " + menuList[i].getMenuName());

            System.out.print("\nSelect an option:");

            input = inputHandler.readOption();

            if(input == -1 || (input<1 && input>menuList.length)){
                System.out.println("Select a valid option!!");
                continue;
            }

            if(menuList[input-1].execute())
                System.out.println("Successful!");
            else
                System.out.println("Sorry, Unsuccessful! (Hint: Login)");

        } while(true);
    }

    public static void main(String[] args) {
        BibliotecaApp instance = new BibliotecaApp();
        try {
            instance.run();
        }
        catch(IOException ioe){
            ioe.printStackTrace();
        }
    }
}
